package com.njym.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.njym.annotation.PermInfo;
import com.njym.entity.SysUser;
import com.njym.service.ReconChannelService;
import com.njym.vo.Json;
import com.njym.vo.ReconChannelVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @program: ReconciliationsController
 * @description:
 * @author: yh
 * @create: 2018-09-12 13:57
 **/
@PermInfo(value = "对账记录接口")
@RestController
@RequestMapping("/ReconChannel")
public class ReconChannelController {

    @Autowired
    ReconChannelService reconchannelService;

    private static final Logger log = LoggerFactory.getLogger(ReconChannelController.class);

    @PostMapping("/listchannel")
    public Json listChannel(@RequestBody String body) {
        String oper = "list channel result";
        log.info("{}, body: {}", oper, body);

        JSONObject json = JSON.parseObject(body);
        Subject currentUser = SecurityUtils.getSubject();
        SysUser user = (SysUser)currentUser.getPrincipal();
        String orgId = user.getOrgId();
        if (orgId.equals("0")) {
            orgId = null;
        }

        String source = json.getString("source");
        BigDecimal totalPlatModeFee = json.getBigDecimal("totalPlatModeFee");
        Integer totalPlatModeCount = json.getInteger("totalPlatModeCount");
        BigDecimal totalOrgModeFee = json.getBigDecimal("totalOrgModeFee");
        Integer totalOrgModeCount = json.getInteger("totalOrgModeCount");

        Object timeInterval = json.get("timeInterval");
        Date start = null;
        Date end = null;
        if (timeInterval != null && timeInterval.getClass() == JSONArray.class) {
            JSONArray timeIntervalarr = (JSONArray)timeInterval;
            start = timeIntervalarr.getTimestamp(0);
            end = timeIntervalarr.getTimestamp(1);
        }
        List<ReconChannelVo> list = reconchannelService.getChannelResult(orgId,source,totalPlatModeFee,totalPlatModeCount,totalOrgModeFee,totalOrgModeCount,start,end);
        System.out.print(list);
        return Json.succ(oper, "list", list );
    }

    @PostMapping("/manualChannel")
    public Json manualChannel(@RequestBody String body) {
        String oper = "list channel result";
        log.info("{}, body: {}", oper, body);

        JSONObject json = JSON.parseObject(body);
        Subject currentUser = SecurityUtils.getSubject();
        SysUser user = (SysUser)currentUser.getPrincipal();
        String orgId = user.getOrgId();
        if (orgId.equals("0")) {
            orgId = null;
        }

        String source = json.getString("source");
        BigDecimal totalPlatModeFee = json.getBigDecimal("totalPlatModeFee");
        Integer totalPlatModeCount = json.getInteger("totalPlatModeCount");
        BigDecimal totalOrgModeFee = json.getBigDecimal("totalOrgModeFee");
        Integer totalOrgModeCount = json.getInteger("totalOrgModeCount");

        Object timeInterval = json.get("value2");
        String time = timeInterval+"";
        String time1=time+" 00:00:00";
        String time2 = time+" 23:59:59";
        Date start = null;
        Date end = null;
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            start = fmt.parse(time1);
            end  = fmt.parse(time2);
        }catch (ParseException e){
            e.printStackTrace();
        }
        List<ReconChannelVo> list = reconchannelService.getChannelResult(orgId,source,totalPlatModeFee,totalPlatModeCount,totalOrgModeFee,totalOrgModeCount,start,end);
        System.out.print(list);
        return Json.succ(oper, "list", list );
    }
}
